//app.js
import touch from './utils/touch.js'

let api = require('/utils/api.js');
App({
  onLaunch: function(options) {
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    var vm = this;
    //获取设备顶部窗口的高度（不同设备窗口高度不一样，根据这个来设置自定义导航栏的高度）
    wx.getSystemInfo({
      success: (res) => {
        let statusBarHeight = res.statusBarHeight,
          navTop = menuButtonObject.top,
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
        this.globalData.navHeight = navHeight;
        this.globalData.navTop = navTop;
        this.globalData.windowHeight = res.windowHeight;
      }
    })
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  globalData: {
    share: false, // 分享默认为false
    height: 0,
    userInfo: null,
    userLocationCity: String, // 城市
    userLocationProvince: String, // 省份,
    userLocation:{
      latitude:String,
      longitude:String
    }
  },
  touch:new touch(),
  checkTokenExprieIn: function(token) {
    var exprieIn = wx.getStorageSync('EXPIREIN')
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    if (exprieIn != '') {
      if (timestamp > exprieIn) {
        wx.navigateTo({
          url: 'pages/login/authUser/index',
        })
      }
    } else {
      wx.navigateTo({
        // url: 'pages/login/login',
        url: 'pages/login/authUser/index',
      })
    }

  },
  // 打电话
  callExpert: function(phone) {
    //// console.log(phone);
    wx.makePhoneCall({
      phoneNumber: phone
    })
    return;
  },
  // 时间戳格式转换
  formatDate: function(time, formatStr) {
    var date = new Date(time);
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
    var D = date.getDate() < 10 ? '0' + (date.getDate()) : (date.getDate());
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    formatStr = formatStr || 'YYYY-MM-DD H:m:s';
    return formatStr.replace(/YYYY|MM|DD|H|m|s/ig, function(matches) {
      return ({
        YYYY: Y,
        MM: M,
        DD: D,
        H: h,
        m: m,
        s: s
      })[matches];
    });
  },
  /**
   * 订单操作
  */
  orderAction: function(data) {
    var param = {
      "id": data.id,
      "remarks": "",
      "state": data.state
    }
    var token=data.token

    return new Promise(function (resolve, reject) {
      api._put('/omc/order/action', param, token).then(res => {
        //// console.log(res);
        if (res.code == 200) {
          
          resolve(res.code);
        } else {
          wx.showToast({
            title: '网络忙请重试',
            icon: 'none'
          })
        }
      })
    
    })

   
  },
  getRemind: function () {
    var token = wx.getStorageSync('TOKEN');
    api._get('/user/remind', '', token).then(res => {
      if (res.code == 200) {
        if (res.result.orderMind > 0) {
          wx.setTabBarBadge({ //购物车不为空 ，给购物车的tabar右上角添加购物车数量标志
            index: 1, //标志添加位置
            text: ".."  //通过编译，将购物车总数量放到这里
          })
        }else{
          wx.removeTabBarBadge({ //购物车不为空 ，给购物车的tabar右上角添加购物车数量标志
            index: 1, //标志添加位置
            // text: "" + res.result.msgMind + "" //通过编译，将购物车总数量放到这里
          })
        }
        if(res.result.msgMind>0){
          wx.setTabBarBadge({ //购物车不为空 ，给购物车的tabar右上角添加购物车数量标志
            index: 2, //标志添加位置
            text: ".."  //通过编译，将购物车总数量放到这里
            // text: "" + res.result.msgMind + "" //通过编译，将购物车总数量放到这里
          })
        }else{
          wx.removeTabBarBadge({ //购物车不为空 ，给购物车的tabar右上角添加购物车数量标志
            index: 2, //标志添加位置
            // text: "" + res.result.msgMind + "" //通过编译，将购物车总数量放到这里
          })
        }

      }
    })
  },
    // 检查是去登录页面还是授权页面
    goAuthpPages() {
      wx.getSetting({
        success(res) {
          console.log(res)
          if (!res.authSetting['scope.userInfo']) {
            wx.navigateTo({
              url: "/pages/login/authUser/index"
            })
          } else {
            wx.navigateTo({
              url: "/pages/login/login"
            })
          }
        }
      })
    },
  
  getUserInfo: function () {
    return new Promise(resolve => {
      var token= wx.getStorageSync('TOKEN')
      var that = this;
      api._get('/user/info', '', token).then(res => {
        if (res.code == 200) {
          wx.setStorageSync('NICKNAME', res.result.nickname);
          wx.setStorageSync('PHONE', res.result.phone);
          wx.setStorageSync('STATE', res.result.state);
          wx.setStorageSync('HEADIMG', res.result.headImg);
          wx.setStorageSync('ACTIVESTATE', res.result.activeState);
      resolve(res.result)
        } else {
          wx.showToast({
            title: '网络忙请重试',
            icon: 'none'
          })
        }
      })
    })
  },
})